<?php
  
  class xml_table_field {
      
      public $tableName;
        public $fieldName;
    public $fieldCaption;
    public $fieldType   = "text";
    public $searchIndex = false;
    public $index       = false;
    public $uniqueIndex = false;
    public $imageResize = "";
    public $required    = false;

    public $tableVisible = true;
    public $formVisible = true;

    public $joinField   = "";
      
      
      
      
    //  private $xml;  
      
      
      
      
      
      
     /* $table, $field, $type = "text",
                               $required = false, $index = false, $uniqueIndex = false, $searchIndex = false*/
      
      
       function addTableField() {
        
           
           
           // Spezialfelder
            if ($this->fieldName == "_user") {
                $this->fieldCaption = "User";
                $this->fieldType = "join";
            }
            
            if ($this->fieldName == "_date") {
                $this->fieldCaption = "Datum";
                $this->fieldType = "date";
            }
            
            if ($this->fieldName == "_time") {
                $this->fieldCaption = "Zeit";
                $this->fieldType = "time";
            }
            


           
           
        /*global $projectPath;

        $filename="$projectPath/db/table.xml";

        global $rootDir;

        $xml     =simplexml_load_file($rootDir . $filename);  */

        /*if ($this->xml == NULL)
            {
            $this->getXml();
            }*/
            
            
            $xmlFile = new xml_file();
            $xmlFile->readXml();
            
            
            //$this->xml = xml_database::readXml();

            
            
            //$xmlTable = xml_database::getTable($this->tableName);
            //$xmlTable->scaffolding
            
            
            
            
            
            
            
            
        $nodes=$xmlFile->xml->xpath('/config/table[@name="' . $this->tableName . '"]/field[@name="' . $this->fieldName . '"]');

        if (!empty($nodes))
            {
            //printf('At least one building named "%s" found', $name);

            }
        else
            {
            //printf('No building named "%s" found', $name);
            //$tableChild=$this->xml->addChild("table", "");
            //   $tableChild->addAttribute("name", $table);


            // echo "nix gefunden";

            //  echo "nix gefunden";

            foreach ($xmlFile->xml->table as $xmlTable)
                {
                //$a           = $xmlTable['name'];

                if ($xmlTable['name'] == $this->tableName)
                    {
                    //echo "found";
                    /*foreach ($xmlTable->field as $xmlTableField)
                    {
                    $tableField[]=(string)$xmlTableField['name'];
                    }*/

                    $tableChild=$xmlTable->addChild("field", "");
                    $tableChild->addAttribute("name", $this->fieldName);
                    $tableChild->addAttribute("type", $this->fieldType);

                
                    
                    if ($this->required)
                        {
                        $tableChild->addAttribute("required", "true");
                        }

                    if ($this->searchIndex)
                        {
                        $tableChild->addAttribute("search_index", "true");
                        }
                    
                    
                    if ($this->uniqueIndex)
                        {
                        $tableChild->addAttribute("unique_index", "true");
                        }
                    
                    
                    
                    
                    
                    }
                }
            }


            $xmlFile->writeXml();
            
            mysql::addTableField($this->tableName, $this->fieldName, $this->fieldType);
            
            if ($this->uniqueIndex) {
                mysql::addUniqueIndex($this->tableName, $this->fieldName);
            }
            
            
            
            if ($this->searchIndex) {
                
                searchindex::addSearchIndex($this->tableName, $this->fieldName);   
                
            }
            
            
            
            
            
            $backendScaffolding = new scaffolding_administration();
            $backendScaffolding->xmlFilename = config::$projectPath . "/db/db.xml";
        $backendScaffolding->baseDir = false;
            $backendScaffolding->createBackend($this->tableName);
                   
            
        //file::createTextFile($filename, $xml->saveXML());
        }

    
    
    
    
    
    
    
    
    
    
    
    function deleteTableField($table, $field) { 
        
        
        
        
    }    
  }
  
  
  
?>
